//
//  ParallaxViewController.m
//  Parallax
//
//  Created by Sidney Just on 14.09.09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "ParallaxViewController.h"

@implementation ParallaxViewController

- (void)movement {
	panMan.panelCamera_x -= 100 * voidDevice.timeStep;
	
	if(starmapPan[0].pos_x < -480)
		starmapPan[0].pos_x = starmapPan[2].pos_x + 480;
	
	if(starmapPan[1].pos_x < -480)
		starmapPan[1].pos_x = starmapPan[0].pos_x + 480;
	
	if(starmapPan[2].pos_x < -480)
		starmapPan[2].pos_x = starmapPan[1].pos_x + 480;
	
	
	if(starmapPan[3].pos_x < -480)
		starmapPan[3].pos_x = starmapPan[5].pos_x + 480;
	
	if(starmapPan[4].pos_x < -480)
		starmapPan[4].pos_x = starmapPan[3].pos_x + 480;
	
	if(starmapPan[5].pos_x < -480)
		starmapPan[5].pos_x = starmapPan[4].pos_x + 480;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	voidDevice = [[VD_Engine alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
	voidDevice.FPSLabelDisplayMode = kDisplayDownLeft;
	
	contentMan = voidDevice.contentManager;
	panMan = voidDevice.panManager;
	
	backgroundPan = [contentMan createPanel:0];
	backgroundPan.parallaxScrolled = YES;
	backgroundPan.texture = [contentMan loadATexture:@"Parallax.png"];
	backgroundPan.atlasPos_x = 0;
	backgroundPan.atlasPos_y = 0;
	backgroundPan.atlasSize_x = 480;
	backgroundPan.atlasSize_y = 320;
	
	starmapPan[0] = [contentMan createPanel:1];
	starmapPan[0].parallaxScrolled = YES;
	starmapPan[0].texture = [contentMan loadATexture:@"Parallax.png"];
	starmapPan[0].atlasPos_x = 0;
	starmapPan[0].atlasPos_y = 320;
	starmapPan[0].atlasSize_x = 480;
	starmapPan[0].atlasSize_y = 320;
	
	starmapPan[1] = [contentMan createPanel:1];
	starmapPan[1].parallaxScrolled = YES;
	starmapPan[1].texture = [contentMan loadATexture:@"Parallax.png"];
	starmapPan[1].atlasPos_x = 0;
	starmapPan[1].atlasPos_y = 320;
	starmapPan[1].atlasSize_x = 480;
	starmapPan[1].atlasSize_y = 320;
	
	starmapPan[2] = [contentMan createPanel:1];
	starmapPan[2].parallaxScrolled = YES;
	starmapPan[2].texture = [contentMan loadATexture:@"Parallax.png"];
	starmapPan[2].atlasPos_x = 0;
	starmapPan[2].atlasPos_y = 320;
	starmapPan[2].atlasSize_x = 480;
	starmapPan[2].atlasSize_y = 320;
	
	starmapPan[3] = [contentMan createPanel:2];
	starmapPan[3].parallaxScrolled = YES;
	starmapPan[3].texture = [contentMan loadATexture:@"Parallax.png"];
	starmapPan[3].atlasPos_x = 0;
	starmapPan[3].atlasPos_y = 640;
	starmapPan[3].atlasSize_x = 480;
	starmapPan[3].atlasSize_y = 300;
	
	starmapPan[4] = [contentMan createPanel:2];
	starmapPan[4].parallaxScrolled = YES;
	starmapPan[4].texture = [contentMan loadATexture:@"Parallax.png"];
	starmapPan[4].atlasPos_x = 0;
	starmapPan[4].atlasPos_y = 640;
	starmapPan[4].atlasSize_x = 480;
	starmapPan[4].atlasSize_y = 300;
	
	starmapPan[5] = [contentMan createPanel:2];
	starmapPan[5].parallaxScrolled = YES;
	starmapPan[5].texture = [contentMan loadATexture:@"Parallax.png"];
	starmapPan[5].atlasPos_x = 0;
	starmapPan[5].atlasPos_y = 640;
	starmapPan[5].atlasSize_x = 480;
	starmapPan[5].atlasSize_y = 300;
	
	
	[contentMan updatePanel:backgroundPan withAtlas:YES forAnimation:NULL];
	[contentMan updatePanel:starmapPan[0] withAtlas:YES forAnimation:NULL];
	[contentMan updatePanel:starmapPan[1] withAtlas:YES forAnimation:NULL];
	[contentMan updatePanel:starmapPan[2] withAtlas:YES forAnimation:NULL];
	[contentMan updatePanel:starmapPan[3] withAtlas:YES forAnimation:NULL];
	[contentMan updatePanel:starmapPan[4] withAtlas:YES forAnimation:NULL];
	[contentMan updatePanel:starmapPan[5] withAtlas:YES forAnimation:NULL];
	[contentMan updatePanel:starmapPan[6] withAtlas:YES forAnimation:NULL];
	
	starmapPan[1].pos_x = 320;
	starmapPan[2].pos_x = 640;
	
	starmapPan[4].pos_x = 320;
	starmapPan[5].pos_x = 640;
	
	[self.view addSubview:voidDevice];
	[voidDevice startAnimation];
	
	[voidDevice scheduleRenderCallAt:self withSelector:@selector(movement)];
}




// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[voidDevice release];
	
    [super dealloc];
}

@end
